using Microsoft.AspNetCore.Authorization;

using WXWorkSvc.Common.Extensions;
using WXWorkSvc.Common.Models;

namespace WXWorkSvc.Third.Areas.D.Pages.User;
public class EditModel : PageModel
{
    private readonly WXDbContext db;
    private readonly WXWork3rdConfig _Config;
    private readonly IRole _Role;
    [BindProperty]
    public TUser Item { get; set; } = default!;
    [BindProperty]
    public List<RoleItem> Items { get; set; } = default!;
    public EditModel(WXDbContext dbc, WXWork3rdConfig config,IRole r)
    {
        db = dbc;
        _Config = config;
        _Role = r;
    }
    public async Task OnGet(string id)
    {
        Item = (await db.Users.FindAsync(_Config.SuiteId, User.GetCorpId(), id))!;
        Items = _Role.GetRoleItems(Item.Roles);
    }
    public async Task<IActionResult> OnPostAsync()
    {
        var roles = _Role.GetRoles(Items);
        
        await db.Users.Where(ii => ii.SuiteId == _Config.SuiteId && ii.CorpId == User.GetCorpId() && ii.UserId == Item.UserId).ExecuteUpdateAsync(ii => ii.SetProperty(jj => jj.Roles, roles));
        return RedirectToPage("./Index");
    }
    //public async Task<IActionResult> OnPostMobileAsync(string id)
    //{
    //    var roles = string.Join("|", Items.Where(ii => ii.Value).Select(ii => ii.Key));
    //    await db.Users.Where(ii => ii.SuiteId == _Config.SuiteId && ii.CorpId == User.GetCorpId() && ii.UserId == id).ExecuteUpdateAsync(ii => ii.SetProperty(jj => jj.Roles, roles));
    //    return RedirectToPage("./IndexM");
    //}
     
}
